import 'package:base_flutter_project/model/cardmodel.dart';
import 'package:base_flutter_project/pages/car_page_detail.dart';
import 'package:flutter/material.dart';
import 'package:base_flutter_project/network/network.dart';

class CardPage extends StatefulWidget {
  @override
  _CardPageState createState() => _CardPageState();
}

class _CardPageState extends State<CardPage> with TickerProviderStateMixin{
  TabController _controller;
  List<CardTabs> tabs;
  String url;

  @override
  void initState() {
    _controller = TabController(length: 0, vsync: this);
    Network.card_fetch().then((res){
      _controller = TabController(length: res.tabs.length, vsync: this);
      url = res.url;
      setState(() {
        tabs = res.tabs;
      });
    }).catchError((e){
      print(e);
    });
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  List<Widget> _createTabs(){
    if(tabs == null) return [];
    return tabs.map((CardTabs tab){
                return Tab(
                  text: tab.labelName,
                );
              }).toList();
  }

  List<Widget> _createTabContainer(){
    if(tabs == null) return [];
    return tabs.map((CardTabs tab){
                return CardPageDetail(travelurl: url,groupChannelCode: tab.groupChannelCode,);
              }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            padding: const EdgeInsets.only(top: 30),
            child: TabBar(
              controller: _controller,
              isScrollable: true,
              labelColor: Colors.black,
              labelPadding: const EdgeInsets.fromLTRB(20, 0, 20, 5),
              indicator: UnderlineTabIndicator(
                borderSide: BorderSide(color: Color(0xff2fcfbb),width: 3),
                insets: const EdgeInsets.only(bottom: 10)
              ),
              tabs: _createTabs(),
            ),
          ),
          Flexible(
            child: TabBarView(
                controller: _controller,
                children: _createTabContainer()
            ),
          )
        ],
      ),
    );
  }
}